<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="房间类型id" prop="roomTypeId">
        <el-input v-model="dataForm.roomTypeId" placeholder="房间类型id"></el-input>
      </el-form-item>
      <el-form-item label="房类名称" prop="roomTypeName">
        <el-input v-model="dataForm.roomTypeName" placeholder="房类名称"></el-input>
      </el-form-item>
      <el-form-item label="会员id" prop="mebId">
        <el-input v-model="dataForm.mebId" placeholder="会员id"></el-input>
      </el-form-item>
      <el-form-item label="会员姓名" prop="mebName">
        <el-input v-model="dataForm.mebName" placeholder="会员姓名"></el-input>
      </el-form-item>
      <el-form-item label="上级id" prop="superiorId">
        <el-input v-model="dataForm.superiorId" placeholder="上级id"></el-input>
      </el-form-item>
      <el-form-item label="上级名称" prop="superiorName">
        <el-input v-model="dataForm.superiorName" placeholder="上级名称"></el-input>
      </el-form-item>
      <el-form-item label="价格" prop="price">
        <el-input v-model="dataForm.price" placeholder="价格"></el-input>
      </el-form-item>
      <el-form-item label="逻辑删除" prop="isDel">
        <el-input v-model="dataForm.isDel" placeholder="逻辑删除"></el-input>
      </el-form-item>
      <el-form-item label="统计日期" prop="date">
        <el-input v-model="dataForm.date" placeholder="统计日期"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="this.isLoading">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        isLoading: false,
        dataForm: {
          id: 0,
          roomTypeId: '',
          roomTypeName: '',
          mebId: '',
          mebName: '',
          superiorId: '',
          superiorName: '',
          price: '',
          isDel: '',
          date: ''
        },
        dataRule: {
          roomTypeId: [
            {required: false, message: '房间类型id不能为空', trigger: 'blur'}
          ],
          roomTypeName: [
            {required: false, message: '房类名称不能为空', trigger: 'blur'}
          ],
          mebId: [
            {required: false, message: '会员id不能为空', trigger: 'blur'}
          ],
          mebName: [
            {required: false, message: '会员姓名不能为空', trigger: 'blur'}
          ],
          superiorId: [
            {required: false, message: '上级id不能为空', trigger: 'blur'}
          ],
          superiorName: [
            {required: false, message: '上级名称不能为空', trigger: 'blur'}
          ],
          price: [
            {required: false, message: '价格不能为空', trigger: 'blur'}
          ],
          isDel: [
            {required: false, message: '逻辑删除不能为空', trigger: 'blur'}
          ],
          date: [
            {required: false, message: '统计日期不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/tra/record/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.roomTypeId = data.record.roomTypeId
                this.dataForm.roomTypeName = data.record.roomTypeName
                this.dataForm.mebId = data.record.mebId
                this.dataForm.mebName = data.record.mebName
                this.dataForm.superiorId = data.record.superiorId
                this.dataForm.superiorName = data.record.superiorName
                this.dataForm.price = data.record.price
                this.dataForm.isDel = data.record.isDel
                this.dataForm.date = data.record.date
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.isLoadingVisible()
            this.$http({
              url: this.$http.adornUrl(`/tra/record/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'roomTypeId': this.dataForm.roomTypeId,
                'roomTypeName': this.dataForm.roomTypeName,
                'mebId': this.dataForm.mebId,
                'mebName': this.dataForm.mebName,
                'superiorId': this.dataForm.superiorId,
                'superiorName': this.dataForm.superiorName,
                'price': this.dataForm.price,
                'isDel': this.dataForm.isDel,
                'date': this.dataForm.date
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.isLoadingVisible()
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
                this.isLoadingVisible()
              }
            })
          }
        })
      },
      isLoadingVisible () {
        this.isLoading = !this.isLoading
      }
    }
  }
</script>
